package cz.zcu.fav.rat.managed.bean.calendar;

import java.util.HashSet;
import java.util.Set;

import org.joda.time.LocalDateTime;

import cz.zcu.fav.rat.beans.PickAction;
import cz.zcu.fav.rat.beans.Reservation;
import cz.zcu.fav.rat.beans.ReturnAction;
import cz.zcu.fav.rat.beans.Period;

public class ReservationFactory
{
	public static Reservation createReservation(ReservationEvent event)
	{
		Reservation reservation = new Reservation();
		reservation.setUser(event.getUser());
		reservation.setObject(event.getObject());
		Set<Period> periods = new HashSet<Period>();
		LocalDateTime since = new LocalDateTime(event.getStartDate());
		LocalDateTime to = new LocalDateTime(event.getEndDate());

		for (int i = 0; i < event.getNumberOfWeeks(); i++)
		{
			Period period = new Period();
			period.setFromDate(since);
			period.setToDate(to);
			period.setPickAction(event.getObject().getObjectClass()
					.isPickable() ? PickAction.NOT_PICKED
					: PickAction.NOT_PICKABLE);
			period.setReturnAction(event.getObject().getObjectClass()
					.isReturnable() ? ReturnAction.NOT_RETURNED
					: ReturnAction.NOT_RETURNABLE);
			period.setValid(true);
			period.setReservation(reservation);
			periods.add(period);
			since = since.plusWeeks(1);
			to = to.plusWeeks(1);
		}
		reservation.setPeriods(periods);

		return reservation;
	}
}
